﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace TunnelsOfDeath
{

    enum ScreenSide {
        LEFT,
        RIGHT
    }

    class ViewPosition : IComparable
    {
        public Vector2 Position { get; set; }
        public Vector2 Velocity { get; set; }
        public ScreenSide Side { get; set; }
        public float slideAnimation { get; set; }
        public Objects.Player FollowedPlayer { get; set; }
        protected int positionX = 0;
        public int DesiredPositionX { get; set; }
        public bool InUse { get; set; }
        public bool InAnimation { get; set; }

        /// <summary>
        /// Returns Vector2 pointing to first pixel of each half of the screen.
        /// </summary>
        /// <param name="_side">Selected side of the screen.</param>
        /// <returns>Vector2</returns>
        public Vector2 GetScreen()
        {
            return new Vector2(positionX, 0);
        }

        public ViewPosition(Vector2 _position, ScreenSide _side)
        {
            Position = _position;
            Velocity = new Vector2(0, 0);
            Side = _side;
            CalculateCentralPosition();
            
        }

        public ViewPosition(Vector2 _position, ScreenSide _side, Objects.Player _followed) : this(_position,_side)
        {
            FollowedPlayer = _followed;
            CalculateCentralPosition();
            InUse = true;

            if (Side == ScreenSide.LEFT)
            {
                DesiredPositionX = 0;
            }
            else
            {
                DesiredPositionX = (int)((Globals.ScreenX / 2)); //start pixel for second half of the screen
                positionX = Globals.ScreenX;
            }
        }

        public int CompareTo(object _second)
        {
            return Comparer<ScreenSide>.Default.Compare(this.Side, ((ViewPosition)_second).Side);
        }

        public void ClearMyArea(SpriteBatch _sb)
        {
            int EndX = Globals.ScreenX;
            int EndY = Globals.ScreenY;

            Rectangle clear = new Rectangle((int)GetScreen().X, (int)GetScreen().Y, EndX, EndY);

            _sb.Draw(Globals.ScreenCleaner, clear, Color.White);
        }


        public void Update()
        {
            if (positionX > DesiredPositionX)
            {
                InAnimation = true;
                positionX -= (int)(10 * Globals.ScreenRatio);
                if (positionX < DesiredPositionX)
                    positionX = DesiredPositionX;

            }
            else if (positionX < DesiredPositionX)
            {
                InAnimation = true;
                positionX += (int)(10 * Globals.ScreenRatio);
                if (positionX > DesiredPositionX)
                    positionX = DesiredPositionX;
            } else if (positionX == DesiredPositionX)
            {
                InAnimation = false;
            }


            if (FollowedPlayer != null)
            {

                Vector2 onGameAreaPosition = new Vector2(FollowedPlayer.position.X * Globals.OBJECTAREA, FollowedPlayer.position.Y * Globals.OBJECTAREA);
                Vector2 onScreenPosition = onGameAreaPosition + Position;
                
                Velocity = new Vector2(0,0);



                if (onScreenPosition.X < 300)
                    Velocity += new Vector2(10, 0);
                if (onScreenPosition.X > 800)
                    Velocity += new Vector2(-10, 0);
                if (onScreenPosition.Y < 300)
                    Velocity += new Vector2(0, 10);
                if (onScreenPosition.Y > 400)
                    Velocity += new Vector2(0, -10);

                Position += Velocity;



            }

        }

        private void CalculateCentralPosition()
        {
            Vector2 centralPosition = new Vector2(Globals.ScreenX / 4, Globals.ScreenY / 2);
        }
    }
}
